package com.ffcc.fitness.card.controller;

import com.ffcc.fitness.base.response.JsonResult;
import com.ffcc.fitness.card.pojo.dto.CardInsertParam;
import com.ffcc.fitness.card.pojo.dto.CardUpdateParam;
import com.ffcc.fitness.card.pojo.vo.CardVO;
import com.ffcc.fitness.card.service.CardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("v1/card/")
@Slf4j
public class CardController {
    
    @Autowired
    private CardService cardService;    @GetMapping("select")
    public JsonResult select() {
        List<CardVO> cardVOList = cardService.selectAllCards();
        return JsonResult.ok(cardVOList);
    }      @GetMapping("selectByCoach")
    public JsonResult selectByCoach(@RequestParam Long coachId) {
        if (coachId == null || coachId <= 0) {
            return new JsonResult(5003, "教练ID不能为空或无效");
        }
        List<CardVO> cardVOList = cardService.selectCardsByCoachId(coachId);
        return JsonResult.ok(cardVOList);
    }//发布器械卡片
    @PostMapping("insert")
    public JsonResult insert(CardInsertParam cardInsertParam){
        cardService.insert(cardInsertParam);
        return JsonResult.ok();
    }    // 更新器械卡片
    @PostMapping("update")
    public JsonResult update(@RequestBody CardUpdateParam cardUpdateParam) {
        if (cardUpdateParam.getCardId() == null || cardUpdateParam.getCardId() <= 0) {
            return new JsonResult(5003, "卡片ID不能为空或无效");
        }
        if (cardUpdateParam.getCoachId() == null || cardUpdateParam.getCoachId() <= 0) {
            return new JsonResult(5003, "教练ID不能为空或无效");
        }
        cardService.update(cardUpdateParam);
        return JsonResult.ok("更新成功");
    }
      // 删除器械卡片
    @PostMapping("delete")
    public JsonResult delete(@RequestParam Long cardId) {
        if (cardId == null || cardId <= 0) {
            return new JsonResult(5003, "卡片ID不能为空或无效");
        }
        cardService.delete(cardId);
        return JsonResult.ok("删除成功");
    }
}
